<?php

class TeachingController extends Controller
{

    public $menuList;

    public $menuitemName;


	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/topColumn';
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
				'minLength'=>4,
				'maxLength'=>4,
                'testLimit'=>1,
            ),
		);
    }


	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
	/*		array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
            ),*/
		);
	}

    /**
     * Display and modify teaching and teaching resources
     * @author : nonglei
     * @date   : 21/03/2014
     */
    public function actionView($id)
    {
        /**
         * ajax to show the different teaching resources info
         */
        if(isset($_POST['ajax']))
        {
            $teachingResource = TeachingResources::model()->findByAttributes(array(
                'teaching_id'=>$_POST['teaching'],
                'resources_id'=>$_POST['resources'],
            ));
            $arr=array(
                'title'=>$teachingResource->title,
                'content'=>$teachingResource->content,
            );
            echo json_encode($arr); // {"a":1,"b":"b","c":true}
            return;
        }
        /**
         * Download resources file
         */
        if(isset($_POST['Download']))
        {
            $resoursesModel = Resources::model()->findByPk($_POST['Download']);
            $resoursesModel->download();
            return;
        }
        $this->render('view',array(
            'model'=>$this->loadModel($id),
		));
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Teaching the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Teaching::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Teaching $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='teaching-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
